# PoP - Policing Socio-Geographic Boundaries and Inequality
# script for creating final dataset for Boston.

#### libraries ####

library(MASS)
library(sp)
library(stargazer)
library(sf)
library(matrixStats)
library(tidyverse)
library(scales)
library(tidycensus)
library(spdep)
library(gridExtra)
library(readxl)

#### downloading census data #### 

# Download Decennial Census data 2010

census_api_key(key = 'f1ad9e98451897912bfdf1227c9a357eaa7d0d78', install = TRUE)

vars_census <- c("P001001", "P012006", "P012007", "P012008", "P012009", "P012010",
                 "P012011", "P012012", "P005003", "P005004", "P005006", "P005010",
                 "P015001", "P005005", "P005007", "P005008", "P005009")

bos_sf1 <- tidycensus::get_decennial(geography = "block", state = "MA",
                                     county = "Suffolk County", variables = vars_census,
                                     year = 2010, output = "wide", geometry = TRUE)

# Download ACS data 2011-2016
# mhhi
# % poverty
# % unemployed
# college

vars_acs <- c("C24010_004E", "C24010_040E", "C24010_001E", "B23025_005E", "B23025_003E",
              "B17021_001E", "B17021_002E", "B15002_011E", "B15002_028E", "B15002_001E",
              "B11003_016E", "B11001_001E", "B25001_001E", "B25003_003E", "B25002_002E",
              "B25002_003E", "B16004_001E", "B16004_006E", "B16004_007E", "B16004_008E",
              "B16004_011E", "B16004_012E", "B16004_013E", "B16004_016E", "B16004_017E",
              "B16004_018E", "B16004_021E", "B16004_022E", "B16004_023E", "B16004_028E",
              "B16004_029E", "B16004_030E", "B16004_033E", "B16004_034E", "B16004_035E",
              "B16004_038E", "B16004_039E", "B16004_040E", "B16004_043E", "B16004_044E",
              "B16004_045E", "B16004_050E", "B16004_051E", "B16004_052E", "B16004_055E",
              "B16004_056E", "B16004_057E", "B16004_060E", "B16004_061E", "B16004_062E",
              "B16004_065E", "B16004_066E", "B16004_067E", "B16004_008E", "B16004_013E",
              "B16004_018E", "B16004_023E", "B16004_030E", "B16004_035E", "B16004_040E",
              "B16004_045E", "B16004_052E", "B16004_057E", "B16004_062E", "B16004_067E",
              "B16004_001E", "B25038_004E", "B25038_011E", "B25038_004E", "B25038_011E",
              "B19013_001", # mhhi 
              "B15003_001", # education universe 
              "B15003_022", "B15003_023", "B15003_024", "B15003_025", # number of college educated or more
              "B25003_001", # homeowner universe
              "B25003_002") # homeowner

vars_loaded = load_variables(dataset = "acs5", year = 2013)
# View(vars_loaded)

bos_acs <- tidycensus::get_acs(geography = "block group", state = "MA",
                               county = "Suffolk County", variables = vars_acs,
                               year = 2013, output = "wide",
                               geometry = TRUE)

# herfindahl or fractionalization index

f_index <- function(x) 1 - rowSums(x^2)

# factor analysis

fac <- function(x, factors, rotation = "varimax", scores = "regression") {
  ok <- complete.cases(x)
  fa <- factanal(x[ok,], factors, rotation=rotation, scores=scores)
  score <- rep(NA, fa$n.obs + sum(!ok))
  score[ok] <- fa$scores
  return(score)
}


# recode Decennial Census

bos_sf1 <- bos_sf1 %>%
  transmute(
    BLK_CODE = GEOID,
    pop = P001001,
    hh = P015001,
    race_white = P005003,
    race_black = P005004,
    race_asian = P005006,
    race_hisp = P005010,
    race_other = P005005 + P005007 + P005008 + P005009,
    p_race_white = race_white / pop,
    p_race_black = race_black / pop,
    p_race_asian = race_asian / pop,
    p_race_hisp = race_hisp / pop,
    p_race_other = race_other / pop,
    age_15_35_male = P012006 + P012007 + P012008 + P012009 + P012010 +
      P012011 + P012012,
    hhi = f_index(cbind(p_race_white, p_race_black, p_race_asian,
                        p_race_hisp, p_race_other)),
    geometry
  )

bos_sf1$GEOID = substring(bos_sf1$BLK_CODE, 1, 12)

# recode ACS

bos_acs <- bos_acs %>%
  transmute(
    BG_CODE = GEOID,
    occ_management = C24010_004E + C24010_040E,
    occ_universe = C24010_001E,
    emp_unemployed = B23025_005E,
    emp_civ_lab_force = B23025_003E,
    poverty_universe = B17021_001E,
    poverty = B17021_002E,
    edu_hs = B15002_011E + B15002_028E,
    edu_universe = B15002_001E,
    hh_single_mother = B11003_016E,
    mhhi = B19013_001E, 
    hh = B11001_001E,
    hu = B25001_001E,
    hu_occupied_renter = B25003_003E,
    hu_occupied = B25002_002E,
    hu_vacant = B25002_003E,
    lang_universe = B16004_001E,
    lang_eng_limited = # Speak Eng less than "very well"
      B16004_006E + B16004_007E + B16004_008E +
      B16004_011E + B16004_012E + B16004_013E +
      B16004_016E + B16004_017E + B16004_018E +
      B16004_021E + B16004_022E + B16004_023E +
      B16004_028E + B16004_029E + B16004_030E +
      B16004_033E + B16004_034E + B16004_035E +
      B16004_038E + B16004_039E + B16004_040E +
      B16004_043E + B16004_044E + B16004_045E +
      B16004_050E + B16004_051E + B16004_052E +
      B16004_055E + B16004_056E + B16004_057E +
      B16004_060E + B16004_061E + B16004_062E +
      B16004_065E + B16004_066E + B16004_067E,
    lang_eng_no = B16004_008E + B16004_013E + B16004_018E + B16004_023E +
      B16004_030E + B16004_035E + B16004_040E + B16004_045E +
      B16004_052E + B16004_057E + B16004_062E + B16004_067E,
    lang_universe = B16004_001E,
    housing_moved_2000_2009 = B25038_004E + B25038_011E,
    housing_moved_2000_2009 = B25038_004E + B25038_011E,
    p_occ_management = occ_management / occ_universe,
    p_emp_unemployed = emp_unemployed / emp_civ_lab_force,
    p_poverty = poverty / poverty_universe,
    p_edu_hs = edu_hs / edu_universe,
    p_single_mother = hh_single_mother / hh,
    p_hu_occupied_renter = hu_occupied_renter / hu_occupied,
    p_hu_vacant = hu_vacant / hu,
    p_lang_eng_limited = lang_eng_limited / lang_universe,
    p_lang_eng_no = lang_eng_no / lang_universe,
    pcol = (B15003_022E + B15003_023E + B15003_024E + B15003_025E) / B15003_001E,
    pown = B25003_002E / B25003_001E,
    p_housing_moved_2000_2009 = housing_moved_2000_2009 / hu_occupied,
    # concentrated disadvantage, residential instability and immigrant concentration
    con_disadv = fac(cbind(p_occ_management, p_emp_unemployed, p_poverty,
                           p_edu_hs, p_single_mother), factors=1),
    res_instab = fac(cbind(p_hu_occupied_renter, p_housing_moved_2000_2009,
                           p_hu_vacant), factors=1),
    immi_con = rowMeans(cbind(p_lang_eng_limited, p_lang_eng_no))
  ) %>% dplyr::select(BG_CODE, con_disadv, res_instab, immi_con, mhhi, pcol, pown, p_poverty,
         p_emp_unemployed)

# join decennial census with ACS data

bos_sf1$BG_CODE = substring(bos_sf1$BLK_CODE, 1, 12)
bos_bg <- bos_sf1 %>% left_join((bos_acs %>% mutate(geometry = NULL) %>% 
                                   as.data.frame), by = "BG_CODE")

# merge with boundary data

bos_shp = read_sf("City_of_Boston_Boundary.shp") %>%
  dplyr::select(geometry)

bos_shp = st_transform(bos_shp, st_crs(bos_bg))
st_crs(bos_shp) = st_crs(bos_bg)

bos_bg = st_make_valid(bos_bg, dist = 0)
bos_shp = st_make_valid(bos_shp, dist = 0)
bos_bg_int = st_intersects(bos_shp, bos_bg)
bos_bg = bos_bg[bos_bg_int[[1]], ]

# wombling, generating difference measure

bos_bg = st_make_valid(bos_bg, dist=0)

uq_bgs = unique(bos_bg$BLK_CODE)

test = poly2nb(bos_bg)
bos_bg$p_race_black_blv = NA
bos_bg$p_race_white_blv = NA
bos_bg$p_race_hisp_blv = NA
bos_bg$p_race_asian_blv = NA

# loop to generate measure

for (i in 1:length(uq_bgs)) {
  
  print(paste0("Iteration ", i))
  
  bos_bg$p_race_black_blv[i] = 
    max(abs(bos_bg[bos_bg$BLK_CODE %in% uq_bgs[i], ]$p_race_black - 
              bos_bg[test[[i]], ]$p_race_black), na.rm = TRUE)
  bos_bg$p_race_white_blv[i] = 
    max(abs(bos_bg[bos_bg$BLK_CODE %in% uq_bgs[i], ]$p_race_white - 
              bos_bg[test[[i]], ]$p_race_white), na.rm = TRUE)
  bos_bg$p_race_hisp_blv[i] = 
    max(abs(bos_bg[bos_bg$BLK_CODE %in% uq_bgs[i], ]$p_race_hisp - 
              bos_bg[test[[i]], ]$p_race_hisp), na.rm = TRUE)
  bos_bg$p_race_asian_blv[i] = 
    max(abs(bos_bg[bos_bg$BLK_CODE %in% uq_bgs[i], ]$p_race_asian - 
              bos_bg[test[[i]], ]$p_race_asian), na.rm = TRUE)
  
}

# now for SES boundaries
# read in categorized 311 data
bos_311_cat <- read_csv("Boston_311_clean.csv")

# filter to 2012-2014 calls since arrests are 2012-2014
# but first format date column
bos_311_cat$created_date <- bos_311_cat$open_dt
bos_311_cat$created_date = substring(bos_311_cat$created_date, 1, 10)
bos_311_cat$created_date = as.Date(bos_311_cat$created_date)

bos_311 = bos_311_cat %>% filter(created_date >= as.Date("2012-01-01") & created_date <=  as.Date("2014-12-31"))

# locate 311 calls within census blocks
# remove missing values from 311 call geometry
bos_311 = bos_311 %>% filter(!is.na(latitude))

# convert 311 call data to sf
bos_311  <- st_as_sf(x = bos_311, coords = c("longitude", "latitude"))

# set crs btw census data and 311 calls
st_crs(bos_311) = st_crs(bos_bg)
bos_311 = st_transform(bos_311, st_crs(bos_bg))

bos_311_merged <- st_join(bos_311,bos_bg,
                          join = st_intersects)

bos_311_merged = bos_311_merged %>% dplyr::select(case_enquiry_id,BLK_CODE, call_code)

# reshape data from long form to wide form with each call code as column
bos_311_merged$val = 1
wide_311_calls = bos_311_merged %>% 
  pivot_wider(names_from = "call_code", values_from = "val", values_fill = 0)

## sum number of each type of call per category by BG and year
wide_311_calls_sum = wide_311_calls %>% 
  group_by(BLK_CODE) %>% 
  summarise(across(c("9","7","12","6","2","5","10","13","8","1","11","4"), sum)) %>% 
  as.data.frame %>% mutate(geometry = NULL)

# sum total calls per year
wide_311_calls_sum = wide_311_calls_sum %>%
  mutate(total_calls = rowSums(across(2:13)))

# sum top 4 categories 
wide_311_calls_sum$high_SES <- wide_311_calls_sum$`5` + wide_311_calls_sum$`10` + wide_311_calls_sum$`13` + + wide_311_calls_sum$`6`
wide_311_calls_sum$high_SES_stan <- (wide_311_calls_sum$high_SES/wide_311_calls_sum$total_calls)

bos_311_final = wide_311_calls_sum

# merge with bos_bg based on BLK_CODE
bos_311_final = bos_311_final %>% dplyr::select(c(BLK_CODE,high_SES_stan))
bos_bg <- merge(bos_bg, bos_311_final, by = "BLK_CODE", all.x = TRUE)

# wombling, generating diff measure between proportion of high SES calls
bos_bg = st_make_valid(bos_bg, dist=0)

uq_bgs = unique(bos_bg$BLK_CODE)

test = poly2nb(bos_bg)
bos_bg$ses_blv = NA

# loop to generate measure

for (i in 1:length(uq_bgs)) {
  
  print(paste0("Iteration ", i))
  
  bos_bg$ses_blv[i] = 
    max(abs(bos_bg[bos_bg$BLK_CODE %in% uq_bgs[i], ]$high_SES_stan - 
              bos_bg[test[[i]], ]$high_SES_stan), na.rm = TRUE)
}

bos_bg$ses_blv = 
  ifelse(bos_bg$ses_blv == Inf | bos_bg$ses_blv == -Inf, NA, bos_bg$ses_blv)

# save bos with boundary measures
save(x = bos_bg, file = "bos_blv.RData")


### data is confidential but providing the code used to aggregate geolocated arrests by census block
### at the end of the code that is commented out, load the dataset with arrests to proceed in the script

#### loading in boston categorized arrest data #### 
# load("bos_categorized_geo.RData")
# 
# ## select necessary columns
# bos_categorized_geo = bos_categorized_geo %>% dplyr::select(-c(BK_DATE,ARREST_LOCATION))
# # convert to sf
# bos_arrests  <- st_as_sf(x = bos_categorized_geo, coords = c("lon", "lat"))
# 
# 
# ## find intersection btw census data and arrest data
# st_crs(bos_arrests) = st_crs(bos_bg)
# bos_arrests = st_transform(bos_arrests, st_crs(bos_bg))
# 
# bos_ints = st_intersects(bos_arrests, bos_bg)
# bos_ints2a = st_intersects(bos_arrests %>% filter(felony == 1), bos_bg)
# bos_ints2b = st_intersects(bos_arrests %>% filter(felony == 0), bos_bg)
# bos_ints2c = st_intersects(bos_arrests %>% filter(violent == 1), bos_bg)
# bos_ints2d = st_intersects(bos_arrests %>% filter(violent == 0), bos_bg)
# bos_ints2e = st_intersects(bos_arrests %>% filter(society == 1), bos_bg)
# bos_ints2f = st_intersects(bos_arrests %>% filter(person == 1), bos_bg)
# bos_ints2g = st_intersects(bos_arrests %>% filter(property == 1), bos_bg)
# 
# 
# 
# theout = bos_bg[bos_ints %>% unlist, ] %>% 
#   dplyr::select(BLK_CODE) %>% 
#   mutate(count = 1) %>% 
#   group_by(BLK_CODE) %>% 
#   summarize(total_arrests = sum(count, na.rm = TRUE)) %>% 
#   as.data.frame %>% 
#   mutate(geometry = NULL)
# 
# theout2a = bos_bg[bos_ints2a %>% unlist, ] %>% 
#   dplyr::select(BLK_CODE) %>%
#   mutate(count = 1) %>% 
#   group_by(BLK_CODE) %>% 
#   summarize(felony_arrests = sum(count, na.rm = TRUE)) %>% 
#   as.data.frame %>% 
#   mutate(geometry = NULL)
# 
# theout2b = bos_bg[bos_ints2b %>% unlist, ] %>% 
#   dplyr::select(BLK_CODE) %>%
#   mutate(count = 1) %>% 
#   group_by(BLK_CODE) %>% 
#   summarize(misdemeanor_arrests = sum(count, na.rm = TRUE)) %>% 
#   as.data.frame %>% 
#   mutate(geometry = NULL)
# 
# theout2c = bos_bg[bos_ints2c %>% unlist, ] %>% 
#   dplyr::select(BLK_CODE) %>%
#   mutate(count = 1) %>% 
#   group_by(BLK_CODE) %>% 
#   summarize(violent_arrests = sum(count, na.rm = TRUE)) %>% 
#   as.data.frame %>% 
#   mutate(geometry = NULL)
# 
# theout2d = bos_bg[bos_ints2d %>% unlist, ] %>% 
#   dplyr::select(BLK_CODE) %>%
#   mutate(count = 1) %>% 
#   group_by(BLK_CODE) %>% 
#   summarize(nonviolent_arrests = sum(count, na.rm = TRUE)) %>% 
#   as.data.frame %>% 
#   mutate(geometry = NULL)
# 
# theout2e = bos_bg[bos_ints2e %>% unlist, ] %>% 
#   dplyr::select(BLK_CODE) %>%
#   mutate(count = 1) %>% 
#   group_by(BLK_CODE) %>% 
#   summarize(society_arrests = sum(count, na.rm = TRUE)) %>% 
#   as.data.frame %>% 
#   mutate(geometry = NULL)
# 
# theout2f = bos_bg[bos_ints2f %>% unlist, ] %>% 
#   dplyr::select(BLK_CODE) %>%
#   mutate(count = 1) %>% 
#   group_by(BLK_CODE) %>% 
#   summarize(person_arrests = sum(count, na.rm = TRUE)) %>% 
#   as.data.frame %>% 
#   mutate(geometry = NULL)
# 
# theout2g = bos_bg[bos_ints2g %>% unlist, ] %>% 
#   dplyr::select(BLK_CODE) %>%
#   mutate(count = 1) %>% 
#   group_by(BLK_CODE) %>% 
#   summarize(property_arrests = sum(count, na.rm = TRUE)) %>% 
#   as.data.frame %>% 
#   mutate(geometry = NULL)
# 
# bos_bg2 = merge(bos_bg, theout, by = "BLK_CODE", all.x = TRUE)
# bos_bg2 = merge(bos_bg2, theout2a, by = "BLK_CODE", all.x = TRUE)
# bos_bg2 = merge(bos_bg2, theout2b, by = "BLK_CODE", all.x = TRUE)
# bos_bg2 = merge(bos_bg2, theout2c, by = "BLK_CODE", all.x = TRUE)
# bos_bg2 = merge(bos_bg2, theout2d, by = "BLK_CODE", all.x = TRUE)
# bos_bg2 = merge(bos_bg2, theout2e, by = "BLK_CODE", all.x = TRUE)
# bos_bg2 = merge(bos_bg2, theout2f, by = "BLK_CODE", all.x = TRUE)
# bos_bg2 = merge(bos_bg2, theout2g, by = "BLK_CODE", all.x = TRUE)
# 
# 
# bos_bg2$total_arrests = ifelse(is.na(bos_bg2$total_arrests), 0, bos_bg2$total_arrests)
# bos_bg2$felony_arrests = ifelse(is.na(bos_bg2$felony_arrests), 0, bos_bg2$felony_arrests)
# bos_bg2$misdemeanor_arrests = ifelse(is.na(bos_bg2$misdemeanor_arrests), 0, bos_bg2$misdemeanor_arrests)
# bos_bg2$violent_arrests = ifelse(is.na(bos_bg2$violent_arrests), 0, bos_bg2$violent_arrests)
# bos_bg2$nonviolent_arrests = ifelse(is.na(bos_bg2$nonviolent_arrests), 0, bos_bg2$nonviolent_arrests)
# bos_bg2$society_arrests = ifelse(is.na(bos_bg2$society_arrests), 0, bos_bg2$society_arrests)
# bos_bg2$person_arrests = ifelse(is.na(bos_bg2$person_arrests), 0, bos_bg2$person_arrests)
# bos_bg2$property_arrests = ifelse(is.na(bos_bg2$property_arrests), 0, bos_bg2$property_arrests)

### read in dataset with aggregate arrests by type to continue
load("bos_block_arrests.RData")

bos_block_arrests <- bos_bg2

#### incorporating crime data ####

# NAD 1983 State Plane Massachusetts
library(readr)
crimebos = read_csv("crime-incident-reports-july-2012-august-2015-source-legacy-system.csv")

state_plane = 
  '+proj=lcc +lat_1=42.68333333333333 +lat_2=41.71666666666667 +lat_0=41 +lon_0=-71.5 +x_0=200000.0001016002 +y_0=750000 +ellps=GRS80 +datum=NAD83 +to_meter=0.3048006096012192 +no_defs'

# 5 % missing 
# prop.table(table(is.na(crimebos$X)))

crimebos = crimebos %>% filter(!is.na(X))
crimebos = crimebos %>% st_as_sf(coords = c("X", "Y"))

st_crs(crimebos) = state_plane
crimebos = st_transform(crimebos, crs = state_plane)
crimebos = st_transform(crimebos, crs = st_crs(bos_bg2$geometry))

# merging 

crimebos$date = 
  as.Date(paste(substring(crimebos$FROMDATE, 7, 10),
                substring(crimebos$FROMDATE, 1, 2),
                substring(crimebos$FROMDATE, 4, 5),
                sep = "-"))

crimebos = crimebos %>% filter(date <= as.Date("2014-10-01"))
crimebos = crimebos %>% mutate(crime = 1)
crimebos = crimebos %>% 
  dplyr::select(crime, geometry)

# figuring out intersection 

st_crs(crimebos) = st_crs(bos_bg2)
crimebos = st_transform(crimebos, crs = st_crs(bos_bg2))

bos_ints3a = st_intersects(crimebos %>% filter(crime == 1), bos_bg2)

theout3a = bos_bg2[bos_ints3a %>% unlist, ] %>% 
  dplyr::select(BLK_CODE) %>%
  mutate(count = 1) %>% 
  group_by(BLK_CODE) %>% 
  summarize(crime = sum(count, na.rm = TRUE)) %>% 
  as.data.frame %>% 
  mutate(geometry = NULL)


bos_bg3 = merge(bos_bg2, theout3a, by = "BLK_CODE", all.x = TRUE)

# add in com_density and phys_bound vars
load("bos_fin_pb3.RData")

# get indicator of physical boundaries by block
bos_phys = bos_fin_pb3 %>% dplyr::select(c(BLK_CODE, phys_bound))
bos_phys = bos_phys %>% as.data.frame() %>% dplyr::select(-c(geometry))

bos_bg3 <- merge(bos_bg3, bos_phys, by = "BLK_CODE")

# integrate commercial density
load("bos_cd.RData")
head(bos_cd)

# Calculate the average total jobs by census block
avg_total_jobs <- bos_cd %>%
  group_by(BLK_CODE) %>%
  summarise(total_jobs = mean(total_jobs, na.rm = TRUE))

bos_bg3 <- merge(bos_bg3, avg_total_jobs, by = "BLK_CODE")

# create com_density measure

bos_bg3$com_density <- bos_bg3$total_jobs/bos_bg3$pop
summary(bos_bg3$com_density)


#### quick edits before saving #### 

# resolving infinite numbers 

bos_bg3 = 
  bos_bg3 %>% 
  mutate(p_race_black_blv = ifelse(p_race_black_blv == Inf | 
                                     p_race_black_blv == -Inf, NA, p_race_black_blv),
         p_race_hisp_blv = ifelse(p_race_hisp_blv == Inf | 
                                    p_race_hisp_blv == -Inf, NA, p_race_hisp_blv),
         p_race_asian_blv = ifelse(p_race_asian_blv == Inf | 
                                     p_race_asian_blv == -Inf, NA, p_race_asian_blv),
         p_race_white_blv = ifelse(p_race_white_blv == Inf | 
                                     p_race_white_blv == -Inf, NA, p_race_white_blv))

# global measure

bos_bg3$edge_race_areal = 
  bos_bg3[, c("p_race_black_blv", "p_race_hisp_blv",
              "p_race_asian_blv", "p_race_white_blv")] %>% 
  mutate(geometry = NULL) %>% 
  as.data.frame %>% 
  as.matrix %>% 
  matrixStats::rowMaxs(x = .)

# pairwise measure 

bos_bg3 = 
  bos_bg3 %>% 
  mutate(edge_race_wb = p_race_black_blv * p_race_white_blv,
         edge_race_wh = p_race_hisp_blv * p_race_white_blv,
         edge_race_hb = p_race_hisp_blv * p_race_black_blv)

# arrest rate; arrest rates by group

bos_bg3$arrest_rate = (bos_bg3$total_arrests / bos_bg3$pop) * 10
bos_bg3$arrest_rate = 
  ifelse(bos_bg3$arrest_rate == Inf | bos_bg3$arrest_rate == -Inf, NA,
         bos_bg3$arrest_rate)
bos_bg3$lpop = log(bos_bg3$pop)

bos_bg3$felony_arrest_rate = (bos_bg3$felony_arrests / bos_bg3$pop) * 10
bos_bg3$felony_arrest_rate = 
  ifelse(bos_bg3$felony_arrest_rate == Inf | bos_bg3$felony_arrest_rate == -Inf, NA,
         bos_bg3$felony_arrest_rate)

bos_bg3$midemeanor_arrest_rate = (bos_bg3$misdemeanor_arrests / bos_bg3$pop) * 10
bos_bg3$midemeanor_arrest_rate = 
  ifelse(bos_bg3$midemeanor_arrest_rate == Inf | bos_bg3$midemeanor_arrest_rate == -Inf, NA,
         bos_bg3$midemeanor_arrest_rate)

bos_bg3$violent_arrest_rate = (bos_bg3$violent_arrests / bos_bg3$pop) * 10
bos_bg3$violent_arrest_rate = 
  ifelse(bos_bg3$violent_arrest_rate == Inf | bos_bg3$violent_arrest_rate == -Inf, NA,
         bos_bg3$violent_arrest_rate)

bos_bg3$nonviolent_arrest_rate = (bos_bg3$nonviolent_arrests / bos_bg3$pop) * 10
bos_bg3$nonviolent_arrest_rate = 
  ifelse(bos_bg3$nonviolent_arrest_rate == Inf | bos_bg3$nonviolent_arrest_rate == -Inf, NA,
         bos_bg3$nonviolent_arrest_rate)

bos_bg3$society_arrest_rate = (bos_bg3$society_arrests / bos_bg3$pop) * 10
bos_bg3$society_arrest_rate = 
  ifelse(bos_bg3$society_arrest_rate == Inf | bos_bg3$society_arrest_rate == -Inf, NA,
         bos_bg3$society_arrest_rate)

bos_bg3$person_arrest_rate = (bos_bg3$person_arrests / bos_bg3$pop) * 10
bos_bg3$person_arrest_rate = 
  ifelse(bos_bg3$person_arrest_rate == Inf | bos_bg3$person_arrest_rate == -Inf, NA,
         bos_bg3$person_arre